<?php

namespace app\adminapi\controller;

use app\model\LinkModel;
use Webman\Http\Request;

class Link extends BaseAdminApi
{
    protected ?string $dataModel = LinkModel::class;
    protected array $with = [];
    protected array $allowSearch = ['title', 'url', 'status', 'type'];

    /**
     * 获取广告详情
     */
    public function getBannerDetail(Request $request)
    {
        $id = $request->get('id');
        
        if (empty($id)) {
            return $this->fail('广告ID不能为空');
        }

        try {
            $banner = LinkModel::find($id);
            
            if (!$banner) {
                return $this->fail('广告不存在');
            }

            return $this->success('获取广告详情成功', $banner);
        } catch (\Exception $e) {
            return $this->fail('获取广告详情失败：' . $e->getMessage());
        }
    }

    /**
     * 创建广告
     */
    public function createBanner(Request $request)
    {
        $data = $request->post();
        
        // 验证必填字段
        if (empty($data['title'])) {
            return $this->fail('广告标题不能为空');
        }

        if (empty($data['url'])) {
            return $this->fail('跳转链接不能为空');
        }

        if (empty($data['cover_img'])) {
            return $this->fail('广告图片不能为空');
        }

        // 验证跳转类型
        $type = isset($data['type']) ? (int)$data['type'] : 1;
        if (!in_array($type, [1, 2, 3])) {
            return $this->fail('跳转类型无效');
        }

        // 当类型为外部小程序时，验证appId
        if ($type == 2) {
            if (empty($data['app_id'])) {
                return $this->fail('外部小程序AppId不能为空');
            }
            if (mb_strlen($data['app_id']) > 50) {
                return $this->fail('AppId不能超过50个字符');
            }
        }

        // 验证字段长度
        if (mb_strlen($data['title']) > 10) {
            return $this->fail('广告标题不能超过10个字符');
        }

        if (mb_strlen($data['url']) > 255) {
            return $this->fail('跳转链接不能超过255个字符');
        }

        if (mb_strlen($data['cover_img']) > 255) {
            return $this->fail('图片链接不能超过255个字符');
        }

        try {
            $banner = new LinkModel();
            $banner->title = $data['title'];
            $banner->url = $data['url'];
            $banner->cover_img = $data['cover_img'];
            $banner->type = $type;
            $banner->app_id = $type == 2 ? $data['app_id'] : null;
            $banner->created_at = date('Y-m-d H:i:s');
            $banner->updated_at = date('Y-m-d H:i:s');
            
            $banner->save();

            return $this->success('创建广告成功', $banner);
        } catch (\Exception $e) {
            return $this->fail('创建广告失败：' . $e->getMessage());
        }
    }

    /**
     * 更新广告
     */
    public function updateBanner(Request $request)
    {
        $data = $request->post();
        
        if (empty($data['id'])) {
            return $this->fail('广告ID不能为空');
        }

        if (empty($data['title'])) {
            return $this->fail('广告标题不能为空');
        }

        if (empty($data['url'])) {
            return $this->fail('跳转链接不能为空');
        }

        if (empty($data['cover_img'])) {
            return $this->fail('广告图片不能为空');
        }

        // 验证跳转类型
        $type = isset($data['type']) ? (int)$data['type'] : 1;
        if (!in_array($type, [1, 2, 3])) {
            return $this->fail('跳转类型无效');
        }

        // 当类型为外部小程序时，验证appId
        if ($type == 2) {
            if (empty($data['app_id'])) {
                return $this->fail('外部小程序AppId不能为空');
            }
            if (mb_strlen($data['app_id']) > 50) {
                return $this->fail('AppId不能超过50个字符');
            }
        }

        // 验证字段长度
        if (mb_strlen($data['title']) > 10) {
            return $this->fail('广告标题不能超过10个字符');
        }

        if (mb_strlen($data['url']) > 255) {
            return $this->fail('跳转链接不能超过255个字符');
        }

        if (mb_strlen($data['cover_img']) > 255) {
            return $this->fail('图片链接不能超过255个字符');
        }

        try {
            $banner = LinkModel::find($data['id']);
            
            if (!$banner) {
                return $this->fail('广告不存在');
            }

            $banner->title = $data['title'];
            $banner->url = $data['url'];
            $banner->cover_img = $data['cover_img'];
            $banner->type = $type;
            $banner->app_id = $type == 2 ? $data['app_id'] : null;
            $banner->updated_at = date('Y-m-d H:i:s');
            
            $banner->save();

            return $this->success('更新广告成功', $banner);
        } catch (\Exception $e) {
            return $this->fail('更新广告失败：' . $e->getMessage());
        }
    }

    /**
     * 删除广告
     */
    public function deleteBanner(Request $request)
    {
        $data = $request->post();
        
        if (empty($data['id'])) {
            return $this->fail('广告ID不能为空');
        }

        try {
            $banner = LinkModel::find($data['id']);
            
            if (!$banner) {
                return $this->fail('广告不存在');
            }
            
            $banner->delete();

            return $this->success('删除广告成功');
        } catch (\Exception $e) {
            return $this->fail('删除广告失败：' . $e->getMessage());
        }
    }

    /**
     * 批量删除广告
     */
    public function batchDeleteBanners(Request $request)
    {
        $data = $request->post();
        
        if (empty($data['ids']) || !is_array($data['ids'])) {
            return $this->fail('请选择要删除的广告');
        }

        try {
            $deleteCount = LinkModel::whereIn('id', $data['ids'])->delete();

            return $this->success('批量删除成功', ['deleted_count' => $deleteCount]);
        } catch (\Exception $e) {
            return $this->fail('批量删除失败：' . $e->getMessage());
        }
    }
}